import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { RoleRight } from 'src/kalend';
import { GameService } from 'src/app/service/game.service';

@Component({
  selector: 'app-luckdraw',
  templateUrl: './luckdraw.component.html',
  styleUrls: ['./luckdraw.component.scss'],
  providers: [GameService]
})
export class LuckdrawComponent implements OnInit {
  user: any = RoleRight.getStorage();
  title: any;
  pageIndex = 1;
  pageSize = 15;
  total = 1;
  dataSet = [];
  loading = true;
  isOkLoading = false;
  isVisible = false;
  doc: any = {};
  status: any;
  statuslist = [{ key: null, value: '全部' }, { key: 1, value: '有效' }, { key: 0, value: '禁用' }];
  winlist = [{ key: true, value: '中奖' }, { key: false, value: '未中奖' }];
  constructor(private service: GameService, private msg: NzMessageService) { }

  ngOnInit() {
    this.searchData();
  }

  searchData(reset: boolean = false): void {
    this.loading = true;
    this.service.querylist({
      offset: this.pageIndex,
      limit: this.pageSize,
      title: this.title,
      status: this.status
    }).then((res) => {
      this.loading = false;
      this.total = res.total;
      this.dataSet = res.rows;
    });
  }
  /**新增 */
  add() {
    this.doc.createuser = this.user._id;
    this.isVisible = true;
  }

  /**修改 */
  edit(id: any) {
    this.isVisible = true;
    this.service.queryById(id).then((res) => {
      this.doc = res;
    });
  }
  /**禁用 */
  lock(id: any) {
    this.service.lock(id).then((res) => {
      this.searchData();
    });
  }
  /**启用 */
  open(id: any) {
    this.service.open(id).then((res) => {
      this.searchData();
    });
  }

  handleOk(): void {
    this.isOkLoading = true;
    if (!this.doc.title || this.doc.title === '') {
      this.msg.warning('请填写活动标题');
    } else if (!this.doc.headtitle) {
      this.msg.warning('请填写活动头标题');
    } else if (!this.doc.gametime) {
      this.msg.warning('请填写活动时间描述');
    } else if (!this.doc.time) {
      this.msg.warning('请填写活动日期');
    } else if (!this.isIntNum(this.doc.time)) {
      this.msg.warning('活动日期必须为数字');
    } else if (!this.doc.gamerule) {
      this.msg.warning('请填写活动规则');
    } else if (!this.doc.count) {
      this.msg.warning('请填写抽奖次数');
    } else if (!this.isIntNum(this.doc.count)) {
      this.msg.warning('抽奖次数必须为数字');
    } else {
      this.service.save(this.doc).then((res) => {
        this.searchData();
        this.msg.success('保存成功！');
        this.isVisible = false;
        this.doc = {};
      });
    }
  }
  handleCancel(): void {
    this.isVisible = false;
    this.doc = {};
  }
  /**
* 校验正负正数就返回true
**/

  isIntNum(val) {
    if (parseFloat(val).toString() === 'NaN') {
      return false;
    } else {
      return true;
    }
  }
}
